(function () {
    function LoaderController() {
        this.Assets = null;
        this.Stage = null;
        this.LoaderUI = null;
        this.DefaultTip = null;
        this.LoadTips = null;

        viewer.register("arena.start", this.start.bind(this));
        viewer.register("arena.resize", this.resize.bind(this));
        viewer.register("loader.load", this.load.bind(this));
    }

    var _proto = LoaderController.prototype;

    /**
     * @privite
     * 资源加载完成以后的回调函数
     * 
     * @param {String} module 资源模块名，可选home或者room
     * @param {any}    params 携带参数，可选
     */
    _proto.onResLoaded = function (module, params) {
        utils.log("加载完成");
        Laya.timer.clearAll(this.LoaderUI);

        switch(module){
            case "home":
                Arena.CurScene = "home";
                viewer.publish("home.loaded", params);
                break;
            case "room":
                Arena.CurScene = "room";
                viewer.publish("room.loaded", params);
                break;
        }
        this.reset();
    }
    /**
     * @privite
     * 资源加载中的回调函数
     * 
     * @param {Number} percent 加载进度，从0到1
     */
    _proto.onResLoading = function (percent) {
        utils.log("加载进度：" + percent);
        this.LoaderUI.progress.value = percent;
    }
    /**
     * @privite
     * 重置加载界面的状态
     */
    _proto.reset = function () {
        var LoaderUI = this.LoaderUI;

        LoaderUI.progress.value = 0;
        LoaderUI.tips.text = this.DefaultTip;
    }
    /**
     * @privite
     * 随机改变加载进度条的提示文字
     * 
     * @param {Array} LoadTips 加载提示文字
     */
    _proto.onLoadTips = function (LoadTips) {
        this.tips.changeText(LoadTips[Math.random() * (LoadTips.length - 1) | 0]);
    }
    /**
     * @public
     * 加载模块资源，加载完成以后会发布xxx.loaded事件
     * 
     * @param {String} module 资源模块名，可选home或者room
     * @param {any}    params 携带参数，可选
     */
    _proto.load = function (module, params) {
        var LoaderUI = this.LoaderUI;

        Arena.Stage.removeChildren();
        Arena.Stage.addChild(LoaderUI);

        switch (module) {
            case "home":
                this.res = this.Assets.HomeRes;
                break;
            case "room":
                this.res = this.Assets.RoomRes;
                break;
        }

        //this.onLoadTips.bind(LoaderUI)(this.LoadTips);
        //Laya.timer.loop(2000, LoaderUI, this.onLoadTips, [this.LoadTips]);
        Laya.loader.load(this.res, Laya.Handler.create(this, this.onResLoaded, [module, params]), Laya.Handler.create(this, this.onResLoading, null, false));
    }
    /**
     * @public
     * 游戏开始时监听arena.start消息，实例化加载界面
     */
    _proto.start = function () {
        this.Assets = Arena.ASSETS;
        this.Stage = Arena.Stage;
        this.LoadTips = this.Assets.LoadTips;
        this.LoaderUI = new ui.Load.LoadPageUI();
        this.LoaderUI.width = Laya.stage.width;
        this.LoaderUI.height = Laya.stage.height;

        this.DefaultTip = this.LoaderUI.tips.text;
    }
    /**
     * @public
     * 监听arena.resize消息，重置界面位置
     */
    _proto.resize = function () {
        if(!this.LoaderUI){return;}
        this.LoaderUI.width = Laya.stage.width;
        this.LoaderUI.height = Laya.stage.height;
    }
    
    new LoaderController();
})();